package com.lance.answer.gui.answer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p><p>
 * <p>
 * 处理学生的考试json
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/7/15 9:34
 */
public class DealStudentTestJsonString {

    public static void main(String[] args) throws Exception {


        //D:\\answer\\一体化\\deal\\
        for (File file : new File("D:\\xinjiang4").listFiles()) {
            System.err.println(file.getName());
            if(file.isDirectory()){
                continue;
            }
            // String dest = "D:\\answer\\一体化\\"+"deal\\"+file.getName();
            String dest = "D:\\xinjiang4\\" + "deal\\" + file.getName();
            try {
            //    fix(new FileInputStream(file), dest);
            //    fix(new FileInputStream("D:\\xinjiang4\\2021年春季学期信息技术与信息管理期末综合试卷-20.json"), "D:\\xinjiang4\\deal\\2021年春季学期信息技术与信息管理期末综合试卷-20.json");
                fix(new FileInputStream("D:\\xinjiang5\\2021年春季学期简明新疆历史期末综合试卷\\&610823199901081446&96.json"), "D:\\xinjiang5\\2021年春季学期简明新疆历史期末综合试卷\\deal" +
                        "\\10823199901081446-20.json");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("error::" + file.getName());
                Thread.sleep(200000);
            }

            Thread.sleep(2000000);

        }


    }

    public static void fix(InputStream inputStream, String dest) throws IOException {

        StringBuilder sb = new StringBuilder();
        for (String s : IOUtils.readLines(inputStream)) {
            sb.append(s);
        }
        System.out.println(sb.toString());



        String result = sb.toString();
        Map<String, Object> content = JSON.parseObject(result, Map.class);

        Map<String, String> map = JSON.parseObject(String.valueOf(content.get("content")), Map.class);

        // 试卷题目信息
        String paperInfo = map.get("paperInfo");
        System.err.println("paperInfo:" + paperInfo);
        Map<String, Object> paperInfoMap = JSON.parseObject(paperInfo, Map.class);

        String sections = String.valueOf(paperInfoMap.get("Sections"));
        System.err.println("Sections:" + sections);
        List<Section> sectionList = JSON.parseArray(sections, Section.class);
        // 问题类型
        Map<String, String> questionTypeMap = Maps.newHashMap();
        Map<String, Integer> questionTypeMapId = Maps.newHashMap();
        for (Section section : sectionList) {
            for (String itemId : section.getItemID()) {
                questionTypeMap.put(itemId, section.getTitle());
                questionTypeMapId.put(itemId, section.getType());
            }
        }

        //  题目答案信息
        System.err.println("map:"+JSON.toJSONString(map));
        String answerInfo = map.get("answerInfo");
        System.err.println("answerInfoString:"+answerInfo);
        System.err.println("answerInfo:" + answerInfo);
        List<AnswerInfo> answerInfoList = JSON.parseArray(answerInfo, AnswerInfo.class);

        // 在这里面判断得分是多少，judgeInfo
        String judgeInfoString = map.get("judgeInfo");
        List<JudgeInfo> judgeInfoList = JSON.parseArray(judgeInfoString, JudgeInfo.class);
        Map<String, JudgeInfo> judgeInfoMapByItemId = Maps.newHashMap();
        judgeInfoList.forEach(e -> judgeInfoMapByItemId.put(e.getI1(), e));


        System.err.println("answerInfoList:" + JSON.toJSONString(answerInfoList));
        Map<String, AnswerInfo> answerInfoMapByItemId = Maps.newHashMap();
        answerInfoList.forEach(e -> answerInfoMapByItemId.put(e.getI1(), e));


        String items = String.valueOf(paperInfoMap.get("Items"));
        System.err.println("items:" + items);
        List<Item> itemList = JSON.parseArray(items, Item.class);

        List<Question> questions = Lists.newArrayList();
        for (Item item : itemList) {


            // I1 itemId
            String itemId = item.getI1();

            JudgeInfo judgeInfo = judgeInfoMapByItemId.get(itemId);
            if(null==judgeInfo||judgeInfo.getI16()==0.0){
                continue;
            }

            // I2 题目
            String tile = item.getI2();
            // I6 答案集合 不是正确答案
            // FIXME 题目类型在Sections里面
            List<String> answers = item.getI6();
            Question question = new Question();
            questions.add(question);
            question.setQuestionCode(itemId);
            question.setQuestionTypeDesc(questionTypeMap.get(itemId));
            question.setQuestionType(questionTypeMapId.get(itemId));
            question.setTitle(tile);
            List<com.lance.answer.dto.Item> questionItems = Lists.newArrayList();

            for (String answer : answers) {
                //  fixme 0是对  1是错  这个要处理一下  而且有的答案里面有标签 需要用正则表达式去掉
                com.lance.answer.dto.Item questionItem = new com.lance.answer.dto.Item(answer, "");
                questionItems.add(questionItem);
            }
            System.err.println("answers"+JSON.toJSONString(answers));

            List<String> correctList = Lists.newArrayList();
            // I15可能为空 也不一定是正确答案，只是用户自己勾选的。
              List<String> i15 = answerInfoMapByItemId.get(itemId).getI15();
              System.err.println("i15:i15"+JSON.toJSONString(i15));

            try{
                // i15里面可能为-1和1，-1的情况说明没有填答案
                for (String s : i15) {
                    int index = Integer.parseInt(s);
                        correctList.add(answers.get(index));
//                    if(answers.size()==2){
//                        // 0对  1就是错
//                        if("-1".equals(s)){
//                            correctList.add("错");
//                        }else {
//                            correctList.add("对");
//                        }
//                    }else {
//                        int index = Integer.parseInt(s);
//                        correctList.add(answers.get(index));
//                    }
                }
            }catch (Exception e){

            }
          System.err.println("correctList:"+correctList);
            String[] correctArray = new String[]{};
            correctArray =correctList.toArray(correctArray);
            System.err.println("correctArray:"+JSON.toJSONString(correctArray));
            question.setCorrectArray(correctArray);
            question.setItems(questionItems);

        }

        List<Map<String, Object>> answers = new ArrayList<>();
        for (Question question : questions) {
            String title = question.getTitle();
            title = Util.dealContent(title);
            Map<String, Object> answerMap = new HashMap<>();
            answerMap.put("questionType", question.getQuestionType());
//            if(question.getQuestionType()==6||question.getQuestionType()==4){
//                answerMap.put("answerType","send");
//            }else {
//                answerMap.put("answerType","click");
//            }

            answerMap.put("answerType", "click");
            answerMap.put("questionTypeDesc", question.getQuestionTypeDesc());
            answerMap.put("questionTitle", title);
            //   answerMap.put("questionCode", MD5Utils.encode(subjectName + title));
            answerMap.put("questionCode", question.getQuestionCode());
            answerMap.put("items", question.getItems());
            answerMap.put("answers", question.getCorrectArray());
            answers.add(answerMap);
        }

        FileUtils.write(new File(dest), JSONObject.toJSONString(answers, SerializerFeature.PrettyFormat), "utf-8");


    }
}
